Yes — looks like we wrote the same code twice.
super
in a Child's Method
Sometimes (as in the example) you want a child class to have its own
method, but that method includes everything the parent's method does.
You can use the super
reference in this situation.
For example,
here is Video's
method:
public String toString() { System.out.println( title + ", " + length + " min. available:" + avail ); }
Here is Movie's
method without using super:
public String toString() { return title + ", " + length + " min. available:" + avail + " dir: " + director + ", rating: " + rating ; }
Movie
's method would better be written using super:
public String toString() { return super.toString() + "dir: " + director + ", rating: " + rating ; }
Unlike the case when super
is used in a constructor,
when super
is used in a method it does not have to be the first statement.
Think of two reasons why using super
in this way is
a good thing to do.